﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;



namespace WebAPI
{

    public class OrderDetail
    {
        public OrderDetail() { }
        public string productName { get; set; }
        public string customer { get; set; }
        public double orderAmount { get; set; }

        public override bool Equals(object obj)
        {
            OrderDetail other = obj as OrderDetail;
            return this.productName == other.productName && this.customer == other.customer && this.orderAmount == other.orderAmount;
        }
    }


    [Serializable]
    public class Order
    {
        public Order() { }
        public Order(long orderID, string productName, string customer, double orderAmount)
        {
            this.orderID = orderID;
            orderDetail = new OrderDetail();
            orderDetail.productName = productName;
            orderDetail.customer = customer;
            orderDetail.orderAmount = orderAmount;
        }
        public long orderID { get; set; }
        public OrderDetail orderDetail;

        public override bool Equals(object obj)
        {
            Order order = obj as Order;
            return this.orderID == order.orderID || this.orderDetail.Equals(order.orderDetail);
        }

        public override string ToString()
        {
            return string.Format("{0,8} {1,10} {2,10} {3,10}", orderID.ToString(), orderDetail.productName, orderDetail.customer, orderDetail.orderAmount.ToString());
            //return string.Format("{0,8} {1,10} {2} {3,10}", orderID.ToString().PadLeft(8,' '), orderDetail.productName.PadLeft(10,' '), orderDetail.customer.PadLeft(10, ' '), orderDetail.orderAmount.ToString().PadLeft(14, ' '));
            //return orderID + " " + orderDetail.productName + " " + orderDetail.customer + " " + orderDetail.orderAmount;
        }


        public int CompareByOrderID(Order other)
        {
            return this.orderID.CompareTo(other.orderID);
        }


        public int CompareByCustomer(Order other)
        {
            return this.orderDetail.customer.CompareTo(other.orderDetail.customer);
        }

        public int CompareByProduct(Order other)
        {
            return this.orderDetail.productName.CompareTo(other.orderDetail.productName);
        }

        public int CompareByAmount(Order other)
        {
            return this.orderDetail.orderAmount.CompareTo(other.orderDetail.orderAmount);
        }


    }

    public class OrderService
    {
        public OrderService()
        {
            orders = new List<Order>();
        }
        public List<Order> orders;


        public List<Order> queryByProduct(string productName)
        {
            List<Order> orderList = new List<Order>();
            for (int i = 0; i < orders.Count; ++i)
            {
                if (orders[i].orderDetail.productName == productName)
                {
                    orderList.Add(orders[i]);
                }
            }
            orderList.Sort((Order x, Order y) => x.CompareByAmount(y));
            return orderList;
        }
        public bool addOrder(Order newOrder)
        {
            for (int i = 0; i < orders.Count; ++i)
            {
                if (orders[i].Equals(newOrder)) return false;
            }
            orders.Add(newOrder);
            orders.Sort((x, y) => x.CompareByOrderID(y));
            return true;
        }

        public bool deleteOrder(long orderID)
        {
            for (int i = 0; i < orders.Count; ++i)
            {
                if (orders[i].orderID == orderID)
                {
                    orders.Remove(orders[i]);
                    return true;
                }
            }
            return false;
        }


        public bool isHaveOrder(long orderID)
        {
            for (int i = 0; i < orders.Count; ++i)
            {
                if (orders[i].orderID == orderID)
                {
                    return true;
                }
            }
            return false;
        }

        public bool modifyOrder(long orderID, string productName, string customer, double orderAmount)
        {
            for (int i = 0; i < orders.Count; ++i)
            {
                if (orders[i].orderID == orderID)
                {
                    orders[i].orderDetail.productName = productName;
                    orders[i].orderDetail.customer = customer;
                    orders[i].orderDetail.orderAmount = orderAmount;
                    return true;
                }
            }
            return false;
        }



        public void Export(string fileName)
        {
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
                using (FileStream fs = new FileStream(fileName, FileMode.Create))
                {
                    xmlSerializer.Serialize(fs, this.orders);
                }
                Console.WriteLine("导出到xml文件成功");
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("导出到xml文件失败");
                Console.WriteLine(ex.Message);
                // Console.WriteLine(ex.StackTrace);
            }
        }


        public void Import(string fileName)
        {
            try
            {
                XmlSerializer xmlSerial = new XmlSerializer(typeof(List<Order>));
                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                {
                    List<Order> orderTmp = (List<Order>)xmlSerial.Deserialize(fs);
                    for (int i = 0; i < orderTmp.Count; ++i)
                    {
                        this.addOrder(orderTmp[i]);
                    }
                    Console.WriteLine("从xml文件导入订单成功");
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("从xml文件导入订单失败");
                Console.WriteLine(ex.Message);
                //Console.WriteLine(ex.StackTrace);
            }
        }

    }
}
